package com.chenjt.netty.five_thrift;

import com.chenjt.netty.five_thrift.generated.Person;
import com.chenjt.netty.five_thrift.generated.PersonService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/12 14:57
 */
public class ThriftClient {

    public static void main(String[] args) throws TTransportException {
        // TFramedTransport 传输层需要和服务端保持一致，超时时间：600ms
        TTransport transport = new TFramedTransport(new TSocket("localhost", 8899), 600);
        // TCompactProtocol 协议层需要和服务端保持一致
        TCompactProtocol protocol = new TCompactProtocol(transport);
        PersonService.Client client = new PersonService.Client(protocol);


        try {
            transport.open();
            Person person = client.getPersonByUsername("张三");

            System.out.println(person.getUsername());
            System.out.println(person.getAge());
            System.out.println(person.isMarried());

            System.out.println("------");

            Person person2 = new Person();
            person2.setUsername("李四");
            person2.setAge(30);
            person2.setMarried(true);

            client.savePerson(person2);

        } catch (TException e) {
            e.printStackTrace();
        } finally {
        }

    }

}
